package com.tgb.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tgb.model.Banner;
import com.tgb.model.group.BannerGroup;
import com.tgb.service.BannerService;
import com.tgb.util.WebTools;

@Controller
@RequestMapping("/banner")
public class BannerController {
	
	@Autowired
	private BannerService bannerService;
	
	/**
	 * 跳转到轮播图首页
	 * @param request
	 * @return
	 */
	@RequestMapping("/getBannerList")
	public String getBannerList(HttpServletRequest request){
		
		BannerGroup bannerGroup = new BannerGroup();
		SimpleDateFormat sdf =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		
		List<Banner> bannerList = bannerService.selectAllBanner();
		if(bannerList != null && bannerList.size() > 0 ){
			
			for(int i = 0; i<bannerList.size();i++){
				
				Banner banner = bannerList.get(i);
				Banner ban = new Banner();
				
				ban.setBannerId(banner.getBannerId());
				ban.setBannerName(banner.getBannerName());
				ban.setBannerDesc(banner.getBannerDesc());
				ban.setPlatDate(sdf.format(banner.getCreateDate()));
				ban.setBannerImgUrl(banner.getBannerImgUrl());
				ban.setBannerLink(banner.getBannerLink());
				
				bannerGroup.getList().add(ban);
				
			}
			
			request.setAttribute("count", bannerList.size());
			request.setAttribute("bannerList", bannerGroup);
		}
		
		return "/banner/banner_list";
	}
	
	/**
	 * 跳转到轮播图详情界面
	 * @param request
	 * @return
	 */
	@RequestMapping("/getBannerDetail")
	public String getBannerDetail(HttpServletRequest request){
		
		return "/banner/banner_detail";
	}
	
	/**
	 * 保存轮播图详情
	 * @param request
	 * @return
	 */
	@RequestMapping("/saveOrEditBanner")
	public String saveOrEditBanner(HttpServletRequest request){
		
//		try{
			
//			int banId = 0;
//		
//			String uuid=WebTools.uuid();
//			String banTitle = getPara("banTitle").trim();
//			int banType = getParaToInt("banType");
//			String banLink = getPara("banLink").trim();
//			String banLinkid = getPara("banLinkid").trim();
//			Date banPlaydate = getParaToDate("playDate");
//			String banSharelink = getPara("banShareLink").trim();
//			String banDesc = getPara("banDesc").trim();
//			int banInsidetype = getParaToInt("banInsidetype");
//			String banInsidelink = getPara("banInsidelink").trim();
//			
//	  	    if(StrKit.notNull(getParaToInt("banId"))){
//	  	    	
//	  	    	banId  = getParaToInt("banId");
//	  		  
//				Banner banner = new Banner();
//				banner.setBanId(banId);
//				banner.setBanTitle(banTitle);
//				banner.setBanType(banType);
//				banner.setBanLink(banLink);
//				banner.setBanLinkid(banLinkid);
//				banner.setBanPlaydate(banPlaydate);
//				banner.setBanSharelink(banSharelink);
//				banner.setBanDesc(banDesc);
//				banner.setBanInsidetype(banInsidetype);
//				banner.setBanInsidelink(banInsidelink);
//				if(isClude(getPara("banAppimgurl")) && StrKit.notBlank(getPara("banAppimgurl"))){
//					
//					banner.setBanAppimgurl(Base64ToImgUtil.getBase64ToImg(getPara("banAppimgurl"), uuid, "jpg"));
//					
//				}
//				if(isClude(getPara("banPcimgurl")) && StrKit.notBlank(getPara("banPcimgurl"))){
//					
//					banner.setBanPcimgurl(Base64ToImgUtil.getBase64ToImg(getPara("banPcimgurl"), uuid+"1", "jpg"));
//					
//				}
//				if(isClude(getPara("banShareimgurl")) && StrKit.notBlank(getPara("banShareimgurl"))){
//					
//					banner.setBanShareimgurl(Base64ToImgUtil.getBase64ToImg(getPara("banShareimgurl"), uuid+"2", "jpg"));
//					
//				}
//				boolean result = banner.update();
//				if(result){
//
//					code = "1";
//					
//				}
//	  	    	
//		  	 }else{
//		  		 
//				Banner banner1 = new Banner();
//				banner1.setBanTitle(banTitle);
//				banner1.setBanType(banType);
//				banner1.setBanLink(banLink);
//				banner1.setBanLinkid(banLinkid);
//				banner1.setBanPlaydate(banPlaydate);
//				banner1.setBanSharelink(banSharelink);
//				banner1.setBanInsidetype(banInsidetype);
//				banner1.setBanInsidelink(banInsidelink);
//				banner1.setBanDesc(banDesc);
//				if(isClude(getPara("banAppimgurl")) && StrKit.notBlank(getPara("banAppimgurl"))){
//					
//					banner1.setBanAppimgurl(Base64ToImgUtil.getBase64ToImg(getPara("banAppimgurl"), uuid, "jpg"));
//					
//				}
//				if(isClude(getPara("banPcimgurl")) && StrKit.notBlank(getPara("banPcimgurl"))){
//					
//					banner1.setBanPcimgurl(Base64ToImgUtil.getBase64ToImg(getPara("banPcimgurl"), uuid+"1", "jpg"));
//					
//				}
//				if(isClude(getPara("banShareimgurl")) && StrKit.notBlank(getPara("banShareimgurl"))){
//					
//					banner1.setBanShareimgurl(Base64ToImgUtil.getBase64ToImg(getPara("banShareimgurl"), uuid+"2", "jpg"));
//					
//				}
//				boolean result = banner1.save();
//				if(result){
//
//					code = "1";
//					
//				}
//				
//		  	 }
//				
//		}catch(Exception e){
//			setAttr("errorMessage", "服务器异常！");
//			e.printStackTrace();
//		}
//		
		
		return "/banner/banner_detail";
	}

}
